package com.ygd.messagedigest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static com.ygd.messagedigest.R.id.md5;

public class MainActivity extends AppCompatActivity {
    private String pw = "ygd123";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    //button点击事件，进行运算
    public void messageDigest(View view) {
        switch (view.getId()){
            case md5://长度是32位
                String md5 = messageDigest("MD5", pw);

                Log.d("flag", "----------->messageDigest: 运算后的结果:" + md5);
                break;

            case R.id.sha1://40位
                String sha1 = messageDigest("SHA-1", pw);
                Log.d("flag", "----------->messageDigest: 经过SHA-1:" +sha1);

                break;
        }
    }

    //运算方法，就创建了
    private String messageDigest(String type, String data) {

        //1、创建消息摘要的对象
        try {

            MessageDigest messageDigest = MessageDigest.getInstance(type);

            //2、调用响应对象的方法,进行计算

            //将要运算的数据，加载给MessageDigest
            messageDigest.update(data.getBytes());

            //hash值
            byte[] digest = messageDigest.digest();

            //3、将字节数组转换成该样式:
            //规律0-9，a-f：十六进制表示数据

            //3.1打印运算后的数据
//            Log.d("flag", "----------->messageDigest: hash值得字节数组长度" +digest.length);
//            for (int i = 0; i < digest.length; i++) {
//                Log.d("flag", "----------->messageDigest" +": "+ digest[i]);
//                Log.d("flag", "----------->messageDigest: 二进制表示:" +Integer.toBinaryString(digest[i]));
//            }
//            11-25 11:31:48.839 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 30
//            11-25 11:31:48.839 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11110
//            11-25 11:31:48.839 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 3
//            11-25 11:31:48.839 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11
//            11-25 11:31:48.839 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 47
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:101111
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: -128
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11111111111111111111111110000000
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: -16
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11111111111111111111111111110000
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: -19
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11111111111111111111111111101101
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 25
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11001
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: -78
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11111111111111111111111110110010
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: -62
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11111111111111111111111111000010
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 18
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:10010
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: -24
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11111111111111111111111111101000
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: -70
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11111111111111111111111110111010
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: -83
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:11111111111111111111111110101101
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 105
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:1101001
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 41
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:101001
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 16
//            11-25 11:31:48.849 32628-32628/com.ygd.messagedigest D/flag: ----------->messageDigest: 二进制表示:10000

            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < digest.length; i++) {
                //0xFF= 二进制：11111111
                //F=15 二进制 ：1111=15
                int num = digest[i] & 0xFF;
                if (num<0xF){
                    builder.append(0);
                }

                String resulet = Integer.toHexString(num);

                builder.append(resulet);


            }


            return builder.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }


        return null;
    }
}
